/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package objects;

import graphic.Color;
import graphic.Graphic;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.Serializable;
import klient.Klient;
import openGL11.Engine;
import org.newdawn.slick.opengl.Texture;

/**
 *
 * @author nicra
 */
public class Triangle implements Serializable{
    public float x[]=new float[3];
    public float y[]=new float[3];
    public float z[]=new float[3];
    public float normal[]=new float[3];
    public float normalx[]=new float[3];
    public float normaly[]=new float[3];
    public float normalz[]=new float[3];
    
    transient public Texture tex;
    public float texx[]=new float[3];
    public float texz[]=new float[3];
    public int number;
    public int type=0;
    public int at=0;
    
    public Color col;
    
    public Triangle(){};
    
    public void toFile(DataOutputStream out){
        try{
            out.writeInt(number);
            out.writeInt(type);
            out.writeInt(at);
            
        for(int i=0;i<3;++i){
            out.writeFloat(x[i]);
            out.writeFloat(y[i]);
            out.writeFloat(z[i]);
            out.writeFloat(normal[i]);
            out.writeFloat(normalx[i]);
            out.writeFloat(normaly[i]);
            out.writeFloat(normalz[i]);
            out.writeFloat(texx[i]);
            out.writeFloat(texz[i]);
            
        }
        }catch(Exception e){}
    }
    public void fromFile(DataInputStream out){
        try{
            number=out.readInt();
            type=out.readInt();
            at=out.readInt();
            
        for(int i=0;i<3;++i){
            x[i]=out.readFloat();
            y[i]=out.readFloat();
            z[i]=out.readFloat();
            normal[i]=out.readFloat();
            normalx[i]=out.readFloat();
            normaly[i]=out.readFloat();
            normalz[i]=out.readFloat();
            texx[i]=out.readFloat();
            texz[i]=out.readFloat();
            
        }
        }catch(Exception e){}
    }
    
    public void setColor(Color col){
        this.col=col;
    }
    
    public void draw(){
        Graphic.e.drawTN(this);
    }
    
    public void drawCol(){
        Graphic.e.setColor(col);
        Graphic.e.drawTN(this);
    }
    
    public Triangle(float x1,float y1,float z1,float x2,float y2,float z2,float x3,float y3, float z3){
        x[0]=x1;
        x[1]=x2;
        x[2]=x3;
        
        y[0]=y1;
        y[1]=y2;
        y[2]=y3;
        
        z[0]=z1;
        z[1]=z2;
        z[2]=z3;
    }
    public Triangle(float x1,float y1,float z1,float x2,float y2,float z2,float x3,float y3, float z3, Color col){
        x[0]=x1;
        x[1]=x2;
        x[2]=x3;
        
        y[0]=y1;
        y[1]=y2;
        y[2]=y3;
        
        z[0]=z1;
        z[1]=z2;
        z[2]=z3;
        
        this.col=col;
    }
    public void calcNormal(){
        float v1[]=new float[3];
        float v2[]=new float[3];
 
	v1[0] = x[0] - x[1];
	v1[1] = y[0] - y[1];
	v1[2] = z[0] - z[1];
 
	v2[0] = x[0] - x[2];
	v2[1] = y[0] - y[2];
	v2[2] = z[0] - z[2];
        
	normal[0] = v1[1]*v2[2] - v1[2]*v2[1]; 
	normal[1] = v1[2]*v2[0] - v1[0]*v2[2]; 
	normal[2] = v1[0]*v2[1] - v1[1]*v2[0]; 
        
        normalize();
    } 
    public void calcNormaln(){
        float v1[]=new float[3];
        float v2[]=new float[3];
 
	v1[0] = x[0] - x[1];
	v1[1] = y[0] - y[1];
	v1[2] = z[0] - z[1];
 
	v2[0] = x[0] - x[2];
	v2[1] = y[0] - y[2];
	v2[2] = z[0] - z[2];
        
	normalx[0]=normalx[1]=normalx[2]=normal[0] = v1[1]*v2[2] - v1[2]*v2[1]; 
	normaly[0]=normaly[1]=normaly[2]=normal[1] = v1[2]*v2[0] - v1[0]*v2[2]; 
	normalz[0]=normalz[1]=normalz[2]=normal[2] = v1[0]*v2[1] - v1[1]*v2[0]; 
        
        normalizeAll();
    }
    public void normalize(){
        float d = (float)Math.sqrt(normal[0]*normal[0]+normal[1]*normal[1]+normal[2]*normal[2]); 
        
	if (d == 0.0) {
            Klient.log("Błąd normalizacji") ;
            return;
	}
 
	normal[0] /= d; normal[1] /= d; normal[2] /= d; 
    }
    public void normalizeAll(){
        normalizeS(0);
        normalizeS(1);
        normalizeS(2);
    }
    public void normalizeS(int i){
        float d = (float)Math.sqrt(normalx[i]*normalx[i]+normaly[i]*normaly[i]+normalz[i]*normalz[i]); 
        
	if (d == 0.0) {
            Klient.log("Błąd normalizacji") ;
            return;
	}
 
	normalx[i] /= d; normaly[i] /= d; normalz[i] /= d; 
    }
    public void normalizeX(){
        float d = (float)Math.sqrt(normalx[0]*normalx[0]+normalx[1]*normalx[1]+normalx[2]*normalx[2]); 
        
	if (d == 0.0) {
            Klient.log("Błąd normalizacji") ;
            return;
	}
 
	normalx[0] /= d; normalx[1] /= d; normalx[2] /= d; 
    }
    public void normalizeY(){
        float d = (float)Math.sqrt(normaly[0]*normaly[0]+normaly[1]*normaly[1]+normaly[2]*normaly[2]); 
        
	if (d == 0.0) {
            Klient.log("Błąd normalizacji") ;
            return;
	}
 
	normaly[0] /= d; normaly[1] /= d; normaly[2] /= d; 
    }
    public void normalizeZ(){
        float d = (float)Math.sqrt(normalz[0]*normalz[0]+normalz[1]*normalz[1]+normalz[2]*normalz[2]); 
        
	if (d == 0.0) {
            Klient.log("Błąd normalizacji") ;
            return;
	}
 
	normalz[0] /= d; normalz[1] /= d; normalz[2] /= d; 
    }
}
